package com.gaokao.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaokao.demo.entity.SysUser;
import com.gaokao.demo.mapper.RoleMapper;
import com.gaokao.demo.mapper.SysUserMapper;
import com.gaokao.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author szs
 * @since 2021-01-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements IUserService {

    @Autowired(required = false)
    private SysUserMapper sysUserMapper;

    @Autowired(required = false)
    private RoleMapper roleMapper;

  /*  *//**
     * 添加用户
     * @param sysUser
     * @return
     *//*
    @Override
    public ResultResponse saveUser(SysUser sysUser) {
        if ("".equals(sysUser.getUsername())) {
            return ResultResponse.newFail("用户名不能为空");
        }
        if ("".equals(sysUser.getPassword())) {
            return ResultResponse.newFail("密码不能为空");
        }
        //已存在用户名
        if (existUser(sysUser.getUsername())) {
            return ResultResponse.newFail("用户名已存在");
        }
        int i = sysUserMapper.insert(sysUser);
        if (i == 1) {
            return ResultResponse.newSuccess();
        }
        return ResultResponse.newFail();
    }*/

    /**
     * 用户是否已存在
     * @param username
     * @return
     */
    @Override
    public boolean existUser(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Integer count = sysUserMapper.selectCount(queryWrapper);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     */
    @Override
    public SysUser findUserByUserName(String username) {
        SysUser sysUser = sysUserMapper.findUserByUserName(username);
        return sysUser;
    }




    @Override
    public IPage<SysUser> getUserList(Integer page,Integer limit,String username,Integer status,String roleId) {
        IPage<SysUser> iPage = new Page<>(page,limit);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (username!=null){
            queryWrapper.like("username", username);
        }
        if (status!=null){
            queryWrapper.eq("status",status);
        }
        if (roleId!=null && roleId!=""){
            queryWrapper.eq("r.id",roleId);
        }
        queryWrapper.eq("u.del_flag",0);
        IPage<SysUser> userList = sysUserMapper.getUserList(iPage, queryWrapper);
        return userList;
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @Override
    public boolean deleteUserById(String id) {
        if (id != null) {
            if (id == "admin") {
                return false;
            }
            int i = this.sysUserMapper.deleteById(id);
            return i == 1 ? true : false;
        }
        return false;
    }

    /**
     * 更新用户信息
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean updateUser(SysUser sysUser) {
        if (sysUser != null && sysUser.getId() != null) {
            SysUser user = this.sysUserMapper.selectById(sysUser.getId());
            this.sysUserMapper.updateById(sysUser);
            this.roleMapper.deleteRoles(user);
            this.roleMapper.insertRoles(sysUser);
            return true;
        }

        return false;
    }

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean addUser(SysUser sysUser) {
        this.sysUserMapper.insert(sysUser);
        this.roleMapper.insertRoles(sysUser);
        return false;
    }


    private Integer updateUserRoleList(SysUser sysUser) {
        if (sysUserMapper.selectById(sysUser.getId()).getSysRoleList() != null) {
            //清空user的角色列表
            roleMapper.deleteRoles(sysUser);
            //插入user中新的角色列表
            Integer i = roleMapper.insertRoles(sysUser);
            return i;
        }
        return null;
    }
}
